#include "picturewidget.h"
#include "ui_picturewidget.h"
#include "pictureitem.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QDebug>
#include <QButtonGroup>
#include <QGraphicsItemAnimation>
#include <QTimeLine>

static QList<pictureItem *> itemList; //图元列表
static QList<QPointF> pointA;
static QMap<int, pictureItem *> mapLink;
static int startNum = 0;
static QSize pictrueBigSize = RAW_VIEW_SIZE/SCALE_VIEW_PIXMAP;

pictureWidget::pictureWidget(QWidget *parent,  QList<QPixmap> *PixmapList) :
    QWidget(parent),
    ui(new Ui::pictureWidget),
    m_timer(new QTimer(this)),
    m_scene(new QGraphicsScene(this)),
    m_index(0),
    m_currentRule(RuleA),
    m_rollCount(0),
    btnMoveEnable(true),
    m_PixmapList(*PixmapList)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_StyledBackground);
    setButtonGroup();
    setInitList();
    setPictureScreen();
    setTimerAndConnect();

    m_newT = new QTimer(this);
    m_newT->setInterval(SWITCHING_INTERVAL);
    connect(m_newT, &QTimer::timeout, [this](){
        btnR_clicked();
    });
    m_newT->start();
}

pictureWidget::~pictureWidget()
{
    m_newT->stop();
    delete ui;
}

void pictureWidget::setButtonGroup()
{
    m_BtnGroup = new QButtonGroup(this);
    m_BtnGroup->addButton(ui->btnPicture0, 0);
    m_BtnGroup->addButton(ui->btnPicture1, 1);
    m_BtnGroup->addButton(ui->btnPicture2, 2);
    m_BtnGroup->addButton(ui->btnPicture3, 3);
    m_BtnGroup->addButton(ui->btnPicture4, 4);
    m_BtnGroup->addButton(ui->btnPicture5, 5);
    m_BtnGroup->addButton(ui->btnPicture6, 6);
    m_BtnGroup->addButton(ui->btnPicture7, 7);
    m_BtnGroup->addButton(ui->btnPicture8, 8);
    m_BtnGroup->addButton(ui->btnPicture9, 9);
    //独占按钮组
    m_BtnGroup->setExclusive(true);
    for (int i = 0; i<10; i++)
    {
        //强转设置按钮id
        static_cast<rotationButton *>(m_BtnGroup->button(i))->setId(i);
    }
}

void pictureWidget::setInitList()
{
    m_PointList << (qreal)0.00 << (qreal)0.15 << (qreal)0.44 << (qreal)0.15 << (qreal)0.15
                << (qreal)0.15 << (qreal)0.15 << (qreal)0.15 << (qreal)0.15 << (qreal)0.15;
    m_ZValueList << 1 << 2 << 1 << 0 << 0 << 0 << 0 << 0 << 0 << 0;
    m_PixmapScaleList << 0.8 << 1 << 0.8 << 0.8 << 0.8 << 0.8 << 0.8 << 0.8 << 0.8 << 0.8;
}

void pictureWidget::setPictureScreen()
{
    ui->graphicsView->setStyleSheet("background: transparent; padding: 0px; border: 0px;");
    //设置场景
    ui->graphicsView->setScene(m_scene);
    m_scene->setSceneRect(0, 0, RAW_VIEW_SIZE.width(), RAW_VIEW_SIZE.height());
    m_MidLine.setPoints(QPointF(0, 0), QPointF(RAW_VIEW_SIZE.width(), 0));

    for(int i = 0; i<10; i++)
    {
        itemList.append(new pictureItem(m_PixmapList[i].scaled(pictrueBigSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
        itemList[i]->setScale(m_PixmapScaleList[i]);
        itemList[i]->setType(i);
        itemList[i]->setId(i);
        itemList[i]->setOffset(QPointF(0, 0));
    }
    //设置图元、位置、优先级
    for(int i = 0; i<10; i++)
    {
        m_scene->addItem(itemList[i]);
        itemList[i]->setPos(m_MidLine.pointAt(m_PointList[i]));
        itemList[i]->setZValue(m_ZValueList[i]);
        itemList[i]->setTransformationMode(Qt::SmoothTransformation);
        if(i != 1)
        {
            itemList[i]->setPos(itemList[i]->x(), RAW_VIEW_SIZE.height()/10);
        }
        pointA.append(itemList[i]->pos());
//        qDebug() << pointA[i].x() << ":" << pointA[i].y();
    }
    for (int i = 0; i<10; i++)
    {
        mapLink.insert(static_cast<rotationButton *>(m_BtnGroup->button(i))->getId(), itemList[i]);
    }

    QMap<int, pictureItem *>::const_iterator it;
    for(it = mapLink.constBegin(); it != mapLink.constEnd(); it++)
    {
//        qDebug() << it.key() << ":" << it.value();
    }
}

void pictureWidget::setTimerAndConnect()
{
    //设置超时时间
    m_timer->setInterval(DURATION_MS/(FPS * DURATION_MS / 1000));
    connect(m_timer, &QTimer::timeout, this, &pictureWidget::timerOutFunc);

    connect(ui->btnR, &QPushButton::clicked, this, &pictureWidget::btnR_clicked);
    connect(ui->btnL, &QPushButton::clicked, this, &pictureWidget::btnL_clicked);

    for(int i = 0; i<10; i++)
    {
        connect(itemList[i], &pictureItem::clickedId, this, &pictureWidget::clickedItemRoll);
        void (rotationButton:: *funcPtr)(int) = &rotationButton::entered;
        connect(static_cast<rotationButton *>(m_BtnGroup->button(i)), funcPtr, [this](int id){
            pictureItem * p = mapLink.value(id);
            btnMoveEnable = false;
            clickedItemRoll(p->getType());
            //当前切换图片type
//            qDebug() << p->getType();
        });
    }
}

void pictureWidget::timerOutFunc()
{
    QVector<QGraphicsItemAnimation *>item(10);

    for(int i = 0; i<10; i++)
    {
        item[i] = new QGraphicsItemAnimation();
    }

    QTimeLine * timeline = new QTimeLine(200);
    //时间线循环次数
    timeline->setLoopCount(1);

    //设置动画
    int first = getrightN(startNum-1);
    for(int i = 0; i<10; i++)
    {
        itemList[i] = mapLink.value(first%10);
        first++;
        itemList[i]->setScale(m_PixmapScaleList[i]);
        itemList[i]->setZValue(m_ZValueList[i]);
        itemList[i]->setTransformationMode(Qt::SmoothTransformation);
        item[i]->setItem(itemList[i]);
        item[i]->setTimeLine(timeline);
        item[i]->setPosAt(1, pointA[i]);
        itemList[i]->setPos(pointA[i]);
    }
    timeline->start();
    //重置
    m_scene->invalidate();
    m_timer->stop();
}

int pictureWidget::getrightN(int num)
{
    if(num == -1)
    {
        num = 9;
    }
    if(num == 10)
    {
        num = 0;
    }

    return num;
}

void pictureWidget::clickedItemRoll(int type)
{
    if(m_timer->isActive())
    {
        return;
    }
    rollItem(type, 0);
}

void pictureWidget::rollItem(int rollDir, unsigned rollCount)
{
    rollCount = 0;
    if(m_timer->isActive())
    {
        return;
    }
    int nbegin = rollDir;
    startNum = getrightN(nbegin);
    m_timer->start();
}

void pictureWidget::btnL_clicked()
{
    int id = m_BtnGroup->checkedId();
    if(id-1 < 0)
    {
        id = 9;
    }
    else
    {
        id--;
    }
    m_BtnGroup->button(id)->setChecked(true);
    rollItem(id, 0);
}

void pictureWidget::btnR_clicked()
{
    int id = m_BtnGroup->checkedId();
    id = (id+1) % 10;
    m_BtnGroup->button(id)->setChecked(true);
    rollItem(id, 0);
}
